<?php
/* @var $this SnapshotController */
/* @var $model Project */

if($project == null){
	$this->breadcrumbs=array(
			Yii::t('monitor','Live List')=>array('index'),
			$county->name
	);
	$displayTitle = $county->name;
} else {
	$this->breadcrumbs=array(
			Yii::t('monitor','Live List')=>array('index'),
			$county->name => array('view', 'cid'=>$county->id),
			$project->name
	);
	$displayTitle = $project->name;
}

?>

<style type="text/css">
#project-grid-live img {
    display: block;
    margin-left: auto;
    margin-right: auto;
}
</style>
<script type="text/javascript" src="http://api.map.baidu.com/api?v=1.5&ak=5a90faa9eadcf6b86a4165ea8f218b47"></script>

<div class="row-fluid hidden-phone">
	<div class="span12">
		<div class="alert alert-info">
			<h1 class="text-center"> <?php echo $displayTitle . Yii::t('monitor','construction site Live'); ?> </h1>
		</div>
	</div>
</div>
<div class="row-fluid">
	<div class="span6">	
<?php 
	if($project == null) {
		//Display the projects list
		$this->widget('bootstrap.widgets.TbGridView',array(
				'id'=>'project-grid-live',
				'type'=>'striped bordered condensed',
				'template'=>"{items}",
				'dataProvider'=>$dataProvider,
				'selectionChanged'=>'function(id){loadSnapshot();}',
				'columns'=>array(
						array(
								'name'=>'name', 'header'=>$county->name,
								'type'=>'raw',
								'value'=>'CHtml::link("$data->name","index.php?r=live/view&cid=".CHtml::encode('.$county->id.')."&pid=".CHtml::encode($data->id))',
								'htmlOptions'=>array('style'=>'text-align:center;cursor:pointer;'), 'headerHtmlOptions'=>array('style'=>'text-align:center')),
				),
		)); 
	} else {
		$cameraNum = count($project->camerases);
		//Display the Live Video here
		$snapshotContent = '';
//		$snapshotContent .= '<div class="alert alert-info">';
// 		$snapshotContent .= '<div class="row-fluid hidden-phone">';
// 		$snapshotContent .= '	<div class="span12">';
// 		$snapshotContent .= '		<div>';
// 		$snapshotContent .= '	   		<h3 class="text-center">' . $county->name . '</h3>';
// 		$snapshotContent .= '	   		<h5 class="text-center">' . $project->name . '</h5>';
// 		$snapshotContent .= '   	</div>';
// 		$snapshotContent .= '   </div>';
// 		$snapshotContent .= '</div>';
		$snapshotContent .= '<div class="row-fluid">';
		$snapshotContent .= '	<div class="span12">';
		if($cameraNum > 0) {
			//http://firealscam1.f3322.org:846/videostream.cgi?user=fireals&pwd=demo
			$snapshotContent .= '<div class="alert alert-info">';
			$snapshotContent .= '<div class="row-fluid">';
			$snapshotContent .= '	<div class="span4">';
			$snapshotContent .= 		Yii::t('project','Select the camera to display video');
			$snapshotContent .= '	</div>';
			$snapshotContent .= '	<div class="span8">';
			$snapshotContent .= '		<div class="btn-toolbar" style="margin: 0;">';
			$snapshotContent .= '			<div class="btn-group">';
			$counter = 1;
			$displayVideoUrl = '';
			foreach($project->camerases as $eachCamera) {
				if($camId == null) {
					if($counter == 1) {
						$snapshotContent .= '			<a href="' . $this->createUrl('view', array('cid'=>$project->county->id, 'pid'=>$project->id, 'camId'=>$eachCamera->id) ) . '"><button class="btn active">' . $counter . '</button></a>';
						$displayVideoUrl = $eachCamera->video_url;
					} else {
						$snapshotContent .= '			<a href="' . $this->createUrl('view', array('cid'=>$project->county->id, 'pid'=>$project->id, 'camId'=>$eachCamera->id) ) . '"><button class="btn">' . $counter . '</button></a>';
					}
				} else {
					if($camId == $eachCamera->id) {
						$snapshotContent .= '			<a href="' . $this->createUrl('view', array('cid'=>$project->county->id, 'pid'=>$project->id, 'camId'=>$eachCamera->id) ) . '"><button class="btn active">' . $counter . '</button></a>';
						$displayVideoUrl = $eachCamera->video_url;
					} else {
						$snapshotContent .= '			<a href="' . $this->createUrl('view', array('cid'=>$project->county->id, 'pid'=>$project->id, 'camId'=>$eachCamera->id) ) . '"><button class="btn">' . $counter . '</button></a>';
					}
				}
				$counter++;
			}
			$snapshotContent .= '			</div>';
			$snapshotContent .= '		</div>';
			$snapshotContent .= '	</div>';
			$snapshotContent .= '</div>';
			$snapshotContent .= '</div>';
			$snapshotContent .= '		<div id="project-grid-live" class="media">';
		    $snapshotContent .= '	   		<img class="media-object img-polaroid" style="-webkit-user-select: none" src="' . $displayVideoUrl . '">';
		    $snapshotContent .= '   	</div>';				
		} else {
			$snapshotContent .= '<div class="text-center">';
			$snapshotContent .= '	   		<p class="text-warning">' . Yii::t('project','No Camera linked.') . '</p>';
			$snapshotContent .= '   	</div>';
		}

	    $snapshotContent .= '   </div>';
	    $snapshotContent .= '</div>';
	    
	    if($cameraNum > 0) {
		    $snapshotContent .= '<div class="row-fluid">';
		    $snapshotContent .= '	<div class="span12">';
		    $snapshotContent .= '		<div class="text-center">';
		    $snapshotContent .= '	   		<p class="text-error">' . Yii::t('common','Minimal Network bandwitdh 150Kbps for viewing Live Video.') . '<br/>' . Yii::t('common','If 3G network used there maybe extra cost produced from Network Operators.') . '</p>';
		    $snapshotContent .= '   	</div>';
		    $snapshotContent .= '   </div>';
		    $snapshotContent .= '</div>';
	    }
	    
	    
//	    $snapshotContent .= '</div>';
	    echo $snapshotContent;
	}

?>		
	</div>
	<div class="span6 hidden-phone">
		<div class="row-fluid">
			<div class="span12">
				<div id="baiduMap" style="width: 100%;min-height: 370px;overflow: hidden;margin:0;"></div>
			</div>
		</div>
		<div class="row-fluid">
			<div class="span12">
				<div id="projectTips" class="alert alert-info" style="text-align:center"><?php echo Yii::t('monitor','Please select project on the map.');?></div>
			</div>
		</div>	</div>
</div>

<script type="text/javascript">
	  	var map = new BMap.Map('baiduMap');
	  	var point = new BMap.Point(101.898517,30.116144);
	    map.centerAndZoom(point, 7);
//	  	map.enableScrollWheelZoom();

	    var bdary = new BMap.Boundary();
	    bdary.get("<?php echo $county->name; ?>", function(rs){       //获取行政区域
        map.clearOverlays();        //清除地图覆盖物       
        var count = rs.boundaries.length; //行政区域的点有多少个
        for(var i = 0; i < count; i++){
            var ply = new BMap.Polygon(rs.boundaries[i], {strokeWeight: 2, strokeColor: "#ff0000"}); //建立多边形覆盖物
            map.addOverlay(ply);  //添加覆盖物
            map.setViewport(ply.getPath());    //调整视野         
        } 
<?php

	$markerIndex = 0;
	foreach($county->projects as $eachData) {
	//	if(count($eachData->camerases) > 0 ) {
			echo 'var marker' . $markerIndex . ' = new BMap.Marker(new BMap.Point(' . $eachData->longitude . ', ' . $eachData->latitude . '));';
			echo 'map.addOverlay(marker' . $markerIndex . ');';
			echo 'var infoWindow' . $markerIndex . ' = new BMap.InfoWindow("' . Yii::t('monitor','Project Description') . ': ' . $eachData->description . '",
																						{width:200,title:"' . $eachData->name . '(<a href=\"' . $this->createUrl('live/view', array('cid'=>$eachData->county->id, 'pid'=>$eachData->id)) . '\">' . Yii::t('monitor','Click to see live.') . '</a>)"});';
			echo 'marker' . $markerIndex . '.addEventListener("click", function(){this.openInfoWindow(infoWindow' . $markerIndex . ');});';
			echo 'marker' . $markerIndex . '.addEventListener("mouseover", function(){changeTips("' . $eachData->name . '"); });';
			echo 'marker' . $markerIndex . '.addEventListener("mouseout", function(){changeTips("' . Yii::t('monitor','Please select project on the map.') . '"); });';
			$markerIndex++;
	//	}
	}
?>
             
	});   
	 
	
	function changeTips(tipContent) {
		$("#projectTips").html(tipContent);
	}
</script>
